<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <input type="text" id="input">
</body>
<script>
  let data = {
    name:'harry'
  }
  let obj = ''
  Object.defineProperty(data,'name',{
    get(){
      console.log('harry被调用了')
      return obj
    },
    set(val){
    console.log('harry被修改了',val)
    document.getElementById('input').value = val
    obj =val
    //当数据层发生改变，视图层也会改变
    //通过set 中的回调参数可以获取到修改后的值
    }
  })
  document.getElementById('input').addEventListener('input',(e)=>{
      data.name =e.target.value
    })
</script>
</html>